package queue;


public class AchieveQueueOfArray1 {

        public int[] queArray;
        public static int length;
        public int rear;
        public int front;
        public int maxSize;

        private AchieveQueueOfArray1(int maxSize){

            this.maxSize = maxSize;
            rear = -1;
            front = 0;
            length = 0;
            queArray = new int[maxSize];
        }

         boolean isFull(){
           return length == maxSize;
        }

        boolean isEmpty(){
            return length == 0;
        }

        //入队
        void insert(int num){
            if(isFull()){
                System.out.println("满了满了 插不进去了");
            }
            length++;
            queArray[++rear] = num;
        }

        //出队
        void outQueue()  throws Exception {
            if(isEmpty()){
                throw new Exception("没有了， 空了");
            }
            if (front == maxSize) {
                front = 0;
            }
            length--;
            front++;
        }

        int seek(){
            if(isEmpty()){
                return -1;
            }
            return queArray[front];
        }

    //显示队列的元素
    public void showQueue() {
        if (isEmpty()){
            throw new RuntimeException("队列空。。。");
        }
        for (int i = front; i <= rear; i++) {
            System.out.printf("queue[%d]=%d\n",i,queArray[i]);
        }
    }

    public static void main(String[] args) throws Exception {
        AchieveQueueOfArray1 que = new AchieveQueueOfArray1(3);
        que.insert(1);
        que.insert(2);
        que.insert(4);

        System.out.println("头元素-------" + que.seek());
        System.out.println("是否为空" + que.isEmpty());
        System.out.println("是否满了" + que.isFull());
        que.showQueue();
        System.out.println("--------------------------------------------");

        que.outQueue();
        que.showQueue();
        System.out.println("头元素" + que.seek());
        System.out.println("是否为空" + que.isEmpty());
        System.out.println("是否满了" + que.isFull());
        System.out.println("--------------------------------------------");

        que.outQueue();
        que.showQueue();
        System.out.println("头元素" + que.seek());
        System.out.println("是否为空" + que.isEmpty());
        System.out.println("是否满了" + que.isFull());
        System.out.println("--------------------------------------------");

        que.outQueue();
//        System.out.println("头元素" + que.seek());
        System.out.println("是否为空" + que.isEmpty());

    }

}
